package com.wj.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.wj.service.WechatService;
import com.wj.tool.common.bean.restful.RestfulBody;
import com.wj.tool.config.wx.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController("WxController")
@RequestMapping(value = "/api/wechat")
@RestfulBody
public class WxController {
    @Resource
    private WechatService wechatService;

    @Resource
    private WxMaService wxService;
    
    @ApiOperation(value = "1.登入接口", httpMethod = "POST")
    @PostMapping("/save")
    public Map<String, Object> login(
            @Validated @RequestBody WechatLoginRequest loginRequest) throws Exception {
 
        Map<String, Object> userInfoMap = wechatService.getUserInfoMap(loginRequest);
        return userInfoMap;
    }

    @GetMapping("/login")
    @ApiOperation("微信登录")
    public Object login(@RequestParam("code") String code){
        return wechatService.login(code);
    }

    /**
     * 保存微信授权的用户数据
     *
     * @return
     */
    @PostMapping(value = "/user")
    @ApiOperation("保存微信授权的用户数据")
    public void saveWeChatUser(@RequestBody @Validated UserRequestVO userRequestVO) {
        wechatService.saveWeChatUser(userRequestVO);
    }

    /**
     * 根据身份证号获取微信用户数据
     *
     * @param identityCard 身份证号
     * @return
     */
    @GetMapping(value = "/id-card")
    @ApiOperation("根据身份证号获取微信用户数据")
    public UserResponseVO getWeChatUserByIdentityCard(@ApiParam(value = "身份证号", required = true) @RequestParam String identityCard) {

        return wechatService.getWeChatUserByIdentityCard(identityCard);
    }


    @GetMapping(value = "/findUser/identityCard")
    @ApiOperation("查询用户是否绑定身份证且登录")
    public WeChatUserVO getWeChatUserByOpenId(@ApiParam(value = "微信openId", required = true) @RequestParam String openId) {
        return wechatService.getWeChatUserByOpenId(openId);
    }

    /**
     * 获取微信用户绑定的手机号相关信息
     *
     * @return
     */
    @GetMapping(value = "/bound-mobile")
    @ApiOperation("获取微信用户绑定的手机号相关信息")
    public MobileResponseVO getBoundMobileNumber(@RequestParam String encryptedData,
                                                 @RequestParam String sessionKey,
                                                 @RequestParam String iv) {

        WxMaUserService wxMaUserService = this.wxService.getUserService();
        // 获取微信用户绑定的手机号相关信息
        WxMaPhoneNumberInfo phoneNoInfo = wxMaUserService.getPhoneNoInfo(sessionKey, encryptedData, iv);
        MobileResponseVO responseVO = new MobileResponseVO();
        responseVO.setCountryCode(phoneNoInfo.getCountryCode());
        responseVO.setPhoneNumber(phoneNoInfo.getPhoneNumber());
        return responseVO;

    }
}